iOS (仿印物App)TableView给力动画的简单实现
撰写于 2016-07-19
修改于 2016-08-20
前言:
之前看见的印物App
里的定制模块的TableView
效果还是不错的,所以仿这个App实现了最主要的亮点之一TableView
滚动效果(层叠效果)。cell
中的细节功能是没有添加的, 需要的话大家可以自己进行扩展添加!
效果图:

功能实现:
首先TableView
与cell
的初始化过程就不多解释了, 接下来我们来看看重要的代码实现:
首先实现cell
中的方法
需要用到2个属性来帮助我们实现这个方法
1 2 3 4 5 6 7 8
| @property (nonatomic, weak)UIView *backGView; @property (nonatomic, weak)UIImageView *backGImage; - (void)cellOffsetOnTabelView:(UITableView *)tabelView;
|
思路:
1.需要规定一个固定的currentLocation
值
2.如果cell
的Y
值小于tabelView.contentOffset.y
值(超出 屏幕上面的位置), 需要进行一次处理(停止cell
偏移)
3.如果cell
的Y
值在currentLocation
范围内需要进行二次处理(进行cell
偏移)
4.最后cell
的Y
值在currentLocation
下面位置进行三次处理(设置初始值), 如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| - (void)cellOffsetOnTabelView:(UITableView *)tabelView { CGFloat currentLocation = tabelView.contentOffset.y + LRLastCellHeight; #if 0 if (currentLocation < LRCellHeight) return; #endif if (self.frame.origin.y < tabelView.contentOffset.y + LRLastCellHeight - LRCellHeight) { self.backGView.height = LRLastCellHeight; self.backGView.y = - (LRLastCellHeight - LRCellHeight); }else if (self.frame.origin.y <= currentLocation && self.frame.origin.y >= tabelView.contentOffset.y) { CGFloat moveY = ABS(self.frame.origin.y - currentLocation) / LRCellHeight * (LRLastCellHeight - LRCellHeight); [self.superview bringSubviewToFront:self]; self.backGView.height = LRCellHeight + moveY; self.backGView.y = - moveY; }else{ self.backGView.height = LRCellHeight; self.backGView.y = 0; } }
|
调用方法
主要核心代码实现完, 其他部分在ViewController
调用就非常简单了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #pragma mark -<UITableViewDataSource> - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { LREffectCell * effectCell = [LREffectCell cellFromTableView:tableView]; return effectCell; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { LREffectCell * effectCell = (LREffectCell *)cell; effectCell.backGImage.image = LRGetImage(indexPath.row); [effectCell cellOffsetOnTabelView:_tableView]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return LRCellHeight; }
|
最后需要监听scrollView
滚动实现cell偏移:
1 2 3 4 5 6 7 8 9
| - (void)scrollViewDidScroll:(UIScrollView *)scrollView { [[_tableView visibleCells] enumerateObjectsUsingBlock:^(LREffectCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [obj cellOffsetOnTabelView:_tableView]; }]; }
|
效果图失帧严重建议去GitHub - 点击下载 运行效果会更明显,
如果喜欢的小伙伴请点一个赞吧,欢迎留言补充与给出不足之处!